//
//  CHBase64.swift
//  yzb
//
//  Created by cyptt on 2022/1/6.
//

import UIKit

public class CHBase64: NSObject {

    /*
     字符串解码
     */
    public static func base64Decoding(base64String:String)->String
    
    {
        let decodedData = NSData(base64Encoded: base64String, options: NSData.Base64DecodingOptions.init(rawValue: 0))
     
        let decodedString = NSString(data: decodedData! as Data, encoding: String.Encoding.utf8.rawValue)! as String
     
        //方法二
//        let decodedData =  Data(base64Encoded: base64String, options: Data.Base64DecodingOptions.init(rawValue: 0))
//        let decodedString = String(data: decodedData, encoding: .utf8)
        
        return decodedString
        
    }
    
    
   /*
    字符串编码
    */
    public static func base64Encoding(plainString:String)->String
    
    {
        let plainData = plainString.data(using: String.Encoding.utf8)
        let base64String = plainData?.base64EncodedString(options: NSData.Base64EncodingOptions.init(rawValue: 0))
        
        //先转换成base64Data
//        let base64Data:Data? = plainData?.base64EncodedData()
//        let base64String2:String? = String(data: base64Data!, encoding: .utf8)
//        print(base64String2)
        
        return base64String!
        
    }
    
    
    /*
     图片转base64
     */
    public static func base64Image(image:UIImage)->String?{
        let data:Data? = image.jpegData(compressionQuality: 1)
        let base64:String? = data?.base64EncodedString(options: Data.Base64EncodingOptions.init(rawValue: 0))
        return base64
    }
    
    /*
     data转base64
     */
    public static func base64Data(data:Data)->String?{
        let base64:String? = data.base64EncodedString(options: Data.Base64EncodingOptions.init(rawValue: 0))
        return base64
    }
   
    
    
   
  

}
